import oss2




def upload_to_oss(file_stream, filename):
    from app import create_app
    app = create_app()
    auth = oss2.Auth(app.config['ACCESS_KEY_ID'], app.config['ACCESS_KEY_SECRET'])
    bucket = oss2.Bucket(auth, app.config['END_POINT'], app.config['BUCKET'])
    result = bucket.put_object(filename, file_stream)
    if result.status == 200:
        return f'https://jdhn.oss-cn-hangzhou.aliyuncs.com/{filename}'
    else:
        raise Exception("File upload to OSS failed")


def batch_upload_to_oss(files):
    urls = []
    for file in files:
        file_url = upload_to_oss(file, file.filename)
        urls.append(file_url)
    return urls
